from django.db import models
from django.utils.translation import gettext_lazy as _
from django.contrib.sites.models import Site
from d115.fields import AutoSlugField
from d115.mixins import *

class NewsItem(AuditMixin):
    language = models.CharField(max_length=12, choices=settings.LANGUAGES, verbose_name=_("Language"), default=settings.LANGUAGE_CODE)
    content = models.TextField(verbose_name=_('Content'))
    link = models.URLField(verify_exists=False, verbose_name=_('Link'), blank=True)
    sites = models.ManyToManyField(Site, verbose_name=_("Sites"))

    class Meta(AuditMixin.Meta):
        verbose_name=_("news item")
        verbose_name_plural=_("news items")
    
class StaticPage(AuditMixin):
    title = models.CharField(max_length=255, verbose_name=_('Title'))
    description = models.TextField(verbose_name=_('Description'), blank=True)
    content = models.TextField(verbose_name=_('Content'), blank=True)
    sidebar = models.TextField(verbose_name=_('Sidebar'), blank=True)
    slug = models.CharField(max_length=255, verbose_name=_("Slug"), editable=True)
    language = models.CharField(max_length=12, choices=settings.LANGUAGES, verbose_name=_("Language"), default=settings.LANGUAGE_CODE)
    status = models.CharField(max_length=255, choices=(("pending", _("Pending")), ("published", _("Published"))), default="pending")
    image = models.ImageField(upload_to="images/", blank=True, null=True)
    image_link = models.CharField(max_length=512, blank=True, null=True)
    sites = models.ManyToManyField(Site, verbose_name=_("Sites"))

    def __unicode__(self):
        return u"%s" % self.title

    class Meta(AuditMixin.Meta):
        verbose_name=_("static page")
        verbose_name_plural=_("static pages")
    
    @models.permalink
    def get_absolute_url(self):
        return ('show-static-page', (), {'slug':self.slug})

class MenuItem(NomenclatureMixin):
    language = models.CharField(max_length=12, choices=settings.LANGUAGES, verbose_name=_("Language"), default=settings.LANGUAGE_CODE)
    url = models.CharField(max_length=512, blank=True, null=True, verbose_name=_("URL"))
    shortcode = models.CharField(max_length=512, blank=True, null=True, verbose_name=_("Short code"))
    on_top = models.BooleanField(default=True, verbose_name=_("Show on top?"))
    in_footer = models.BooleanField(default=False, verbose_name=_("Show in footer?"))
    sites = models.ManyToManyField(Site, verbose_name=_("Sites"))

    class Meta(NomenclatureMixin.Meta):
        verbose_name = _("menu item")
        verbose_name_plural = _("menu items")

    def get_absolute_url(self):
        return self.url

class SiteSetting(NomenclatureMixin):
    site = models.ForeignKey(Site, verbose_name=_("Site"))
    value = models.TextField(blank=True, verbose_name=_("Value"))
    is_html = models.BooleanField(default=False)
    use_breaks = models.BooleanField(default=True)

    def __unicode__(self):
        return u"%s" % self.value

    class Meta(NomenclatureMixin.Meta):
        verbose_name = _("site setting")
        verbose_name_plural = _("site settings")
